$(document).ready(function() {
	
	$('#btn_login').on('click', function() {
		var data = {};
		$('#frm_login input[name]').each(function() {
			data[this.getAttribute('name')] = this.value;
		});
		$.ajax({
			url : '/logon',
			type : 'post',
			contentType : 'application/json;charset=utf-8',
			data : JSON.stringify(data),
			timeout : 5000,
			dataType : 'json',
			success : function(data, textStatus) {
				
				if(data.code == 1){
					
					window.location.href="/index.html";
				}else{
					$("#login_message b").empty();
					$('#login_message b').append(data.message);
				}

			},
			error : function(xhr, textStatus) {
				console.log(xhr)
				console.log(textStatus)
			}
		})
	});	
	
	$('#btn_reg').on('click', function() {
		var data = {};
		var rePassword = "";
		var password = "";
		$('#frm_reg input[name]').each(function() {
			data[this.getAttribute('name')] = this.value;
			if('rePassword' == this.getAttribute('name')){
				
				rePassword = this.value;
			}
			
			if('password' == this.getAttribute('name')){
				
				password = this.value;
			}
		});
		
		if(rePassword != password) {

			$("#reg_message").empty();
			$('#reg_message').append("<b>Please confirm your password.</b>");
			return false;
		}

		$.ajax({
			url : '/register',
			type : 'post',
			contentType : 'application/json;charset=utf-8',
			data : JSON.stringify(data),
			timeout : 5000,
			dataType : 'json',
			success : function(data, textStatus) {

				if(data.code == 1){
					
					window.location.href="/login.html";
				}else{

					$("#reg_message").empty();
					$('#reg_message').append(data.message);
				}

			},
			error : function(data, textStatus) {
				console.log(xhr)
				console.log(textStatus)
			}
		})
	});
});